#!/bin/bash

if [ `command -v docker` ];then
    echo 'sucess: docker is install !'
else
    echo 'error: please install docker !'
    exit
fi

# 判断是否安装k3s
if [ `command -v k3s` ];then
    echo 'k3s has installed'
else
    export K3S_NODE_NAME=${HOSTNAME//_/-}
    export INSTALL_K3S_EXEC="--docker --kube-apiserver-arg service-node-port-range=1-65000 --no-deploy traefik --write-kubeconfig ~/.kube/config --write-kubeconfig-mode 666"
    curl -fsL https://docs.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -
fi

#所有node节点安装nfs客户端
if [ -r /etc/redhat-release ];then
    yum -y install nfs-utils
    systemctl start nfs && systemctl enable nfs
else
    apt-get -y install nfs-kernel-server
    systemctl start nfs-kernel-server && systemctl enable nfs-kernel-server
fi

# docker 安装 rancher
docker run -d -v /data/docker/rancher-server/var/lib/rancher/:/var/lib/rancher/ --restart=unless-stopped --name rancher-server -p 9080:80 -p 9443:443 rancher/rancher:stable

echo 'success: finish & need reboot'
echo "export K3S_TOKEN=$(cat /var/lib/rancher/k3s/server/node-token)"
# echo "export K3S_URL=https://$(ifconfig eth0 |grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"):6443"
